# -*- coding: utf-8 -*-
"""
Created on Sat Jul 29 07:02:54 2023

@author: skunk69
"""

from pathlib import Path,PurePath
import json
from text_align import count_char,chinese_align,space_align

file_name = 'SCL-90.json'
input_path = PurePath(Path.cwd().parent,'json_docs',file_name)
with open(input_path, encoding='utf-8') as json_file:
    this_scale = json.load(json_file)

chinese_name = this_scale['chinese_name']
english_name = this_scale['english_name']
abbreviation = this_scale['abbreviation']
outline = this_scale['outline']
contents = this_scale['contents']

instruction = contents['instruction']
items = contents['items']
reverse_items = contents['reverse_items']
scales = contents['scales']
scales_items = contents['scales_items']
factors = contents['factors']
factors_scales = contents['factors_scales']
rating = contents['rating']
score_rules = contents['score_rules']

implementation = this_scale['implementation']
measurements = this_scale['measurements']
interpretation = this_scale['interpretation']
applications = this_scale['applications']

# output _guide.rst    
with open(this_scale['abbreviation']+'_guide.rst','w+') as f_guide:
    
    line = f'{"="*len(chinese_name)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{chinese_name}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    english = english_name+', '+abbreviation
    f_guide.write(f'{english}\n')
    f_guide.write('\n')
    
    subtitle = u'概述'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    lines = outline.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n')
    
    subtitle = u'内容'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    f_guide.write(f'{abbreviation}量表包括了{len(items)}条条目，')
    if reverse_items:
        f_guide.write(f'反向条目包括{reverse_items}，')
    f_guide.write(f'{len(scales)}个维度，这些维度分别是：')
    for num,scale in enumerate(scales):
        if num < len(scales)-1:
            f_guide.write(f'{scale}，')
        else:
            f_guide.write(f'{scale}。')
    
    f_guide.write('其中，')
    for num,scale in enumerate(scales):
        if num < len(scales)-1:
            f_guide.write(f'{scale}维度由条目{scales_items[num]}组成，')
        else:
            f_guide.write(f'{scale}维度由条目{scales_items[num]}组成。')
    
    if factors:
        f_guide.write(f'这些维度又综合为{len(factors)}个因子，{tuple(factors)}。')
        for num,factor in enumerate(factors):
            if num < len(factors)-1:
                f_guide.write(f'{factor}因子由维度{tuple(factors_scales[num])}组成，')
            else:
                f_guide.write(f'{factor}因子由维度{tuple(factors_scales[num])}组成。')
                
    f_guide.write('评价准则为：') 
    for num,rate in enumerate(rating):
        if num < len(rating)-1:
            f_guide.write(f'{rating[num]},')
        else:
            f_guide.write(f'{rating[num]}。')
    
    f_guide.write('计分规则为：') 
    for num,rate in enumerate(rating):
        if num < len(rating)-1:
            f_guide.write(f'{rating[num]}计{score_rules[num]}分，')
        else:
            f_guide.write(f'{rating[num]}计{score_rules[num]}分。\n')
    f_guide.write('\n')
    
    subtitle = u'实施方法'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    lines = implementation.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n')
     
    subtitle = u'测量学指标'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')
    
    reliability = measurements['reliability']
    validity = measurements['validity']
    
    f_guide.write('量表信度::\n')
    f_guide.write('\n')
    f_guide.write(f'  该量表的信度数据：{reliability}\n')
    f_guide.write('\n')
    f_guide.write('量表效度::\n')
    f_guide.write('\n')
    f_guide.write(f'  该量表的效度数据：{validity}\n')
    f_guide.write('\n')
       
    subtitle = u'结果分析方法'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')    
    
    lines = interpretation.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n') 
    
    subtitle = u'应用情况'
    line = f'{"-"*len(subtitle)*2}\n'
    f_guide.write(line)
    f_guide.write(f'{subtitle}\n')
    f_guide.write(line)
    f_guide.write('\n')    
    
    lines = applications.split('\n')
    for line in lines:
        f_guide.write(f'{line}\n')    
        f_guide.write('\n')
    
    f_guide.close() 
        
# output _scale.rst    
with open(this_scale['abbreviation']+'_scale.rst','w+') as f_scale:
    
    line = f'{"="*len(chinese_name)*2}\n'
    f_scale.write(line)
    f_scale.write(f'{chinese_name}\n')
    f_scale.write(line)
    f_scale.write('\n')
    
    english = english_name+', '+abbreviation
    f_scale.write(f'{english}\n')
    f_scale.write('\n')
    
    subtitle = u'指导语'
    line = f'{"-"*len(subtitle)*2}\n'
    f_scale.write(line)
    f_scale.write(f'{subtitle}\n')
    f_scale.write(line)
    f_scale.write('\n')
    
    lines = instruction.split('\n')
    for line in lines:
        f_scale.write(f'{line}\n')
    
    f_scale.write('\n')

    # construction of RST table with grid table
    L1 = 50 # items column
    L2 = 6 # one of rating column
    
    # first format line of RST table
    plus = '+'
    format_line = plus+'-'*L1+plus
    for rate in rating:
        format_line = format_line+'-'*L2+plus
    f_scale.write(f'{format_line}\n')

    # header line
    rating = space_align(rating)
    Lmax_items = max([count_char(value) for value in items.values()])
    Lmax_rating = max([count_char(rate) for rate in rating])
    print(f'Lmax_items={Lmax_items}')
    print(f'Lmax_rating={Lmax_rating}')
    print(f'rating={rating}')
    
    half = int(Lmax_rating/2)

    bar = '|'
    header_line = bar+f'{" ":<{L1}}'+bar
    for rate in rating:
        LL = count_char(rate[0:half])
        header_line = header_line+f'{rate[0:half]:<{L2-LL}}'+bar
        
    f_scale.write(f'{header_line}\n')

    header_line = bar+f'{" ":<{L1}}'+bar
    for rate in rating:
        LL = count_char(rate[half::])
        header_line = header_line+f'{rate[half::]:<{L2-LL}}'+bar    
    
    f_scale.write(f'{header_line}\n')

    sep_line = plus+'='*L1+plus
    for rate in rating:
        sep_line = sep_line+'='*L2+plus
    f_scale.write(f'{sep_line}\n')

    # table contents   
    longstr = """：山有峰顶，海有彼岸。漫漫长途，终有回转。余味苦涩，终有回甘。"""
    longstr = ''
    rate_line = ''
    for rate in rating:
        rate_line = rate_line+' '*L2+bar
    
    for key,value in items.items():
        value = key+'.'+value+longstr # example
        LL = count_char(value)
        if LL+len(value) > L1:
            LL1 = count_char(value[0:int(L1/2)])
            line = bar+f'{value[0:int(L1/2)]:<{L1-LL1}}'+bar+rate_line
            f_scale.write(f'{line}\n')
            LL1 = count_char(value[int(L1/2)::])
            line = bar+f'{value[int(L1/2)::]:<{L1-LL1}}'+bar+rate_line
            f_scale.write(f'{line}\n')
        else:
            line = bar+f'{value:<{L1-LL}}'+bar+rate_line
            f_scale.write(f'{line}\n')
        f_scale.write(f'{format_line}\n')
    
    f_scale.close()